<template>
  <div>
    <el-dialog :title="proDate.id ? '编辑房产' : '添加房产'" :visible="showDialog" @close="clonse">
      <el-form ref="form" label-width="120px" :model="proDate" :rules="rules">
        <el-form-item label="经纪">
          <el-select v-model="proDate.agent">
            <el-option v-for="item in allAgent " :key="item.id" :value="proDate.id?proDate.agent:item.id" :label=" proDate.id ? proDate.agent.name:item.name" :placeholder="proDate.agent" />
          </el-select>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="proDate.category">
            <el-option v-for="item in allCategory" :key="item.id" :value="proDate.id?proDate.category: item.id" :label="proDate.id?proDate.category.title:item.title" />
          </el-select>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="proDate.title" />
        </el-form-item>
        <el-form-item label="标签">
          <el-tag
            v-for="tag in proDate.tags"
            :key="tag"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >
            {{ tag }}
          </el-tag>
          <el-input
            v-if="inputVisible"
            ref="saveTagInput"
            v-model="inputValue"
            class="input-new-tag"
            size="small"
            @blur="handleInputConfirm"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showInput">+ 新标签</el-button>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="proDate.address" />
        </el-form-item>
        <el-form-item label="价格">
          <el-input v-model="proDate.price" />
        </el-form-item>
        <el-form-item label="描述" prop="desc">
          <el-input v-model="proDate.desc" type="textarea" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <quillEditor v-model="proDate.content" />
        </el-form-item>
        <el-form-item label="介绍图片">
          <!-- <el-input @change="inputUrl" v-model="proDate.photos" /> -->
        </el-form-item>
        <el-form-item label="封面">
          <el-input v-model="proDate.image" placeholder="请输入图片路径" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row type="flex" justify="center">
          <el-button size="small" type="primary" @click="submit">确认</el-button>
          <el-button size="small" @click="clonse">取消</el-button>
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addPropertyInfo, getPropertyInfo, editPropertyInfo } from '@/api/property'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  components: { quillEditor },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    allCategory: {
      type: Array,
      default: () => []
    },
    allAgent: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      proDate: {
        title: '',
        desc: '',
        image: '',
        agent: '',
        content: '',
        address: '',
        price: '',
        photos: [],
        tags: [],
        category: ''
      },
      inputVisible: false,
      inputValue: '',
      dialogVisible: false,
      inputImg: '',
      number: 0,
      rules: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    inputUrl(val) {
      console.log(val)
    },
    // 数据回显
    async getPropertyInfo(id) {
      const res = await getPropertyInfo(id)
      // console.log(res)
      this.proDate = res.data
    },
    // 添加编辑的确认按钮
    async submit() {
      await this.$refs.form.validate()
      if (this.proDate.id) {
        await editPropertyInfo(this.proDate)
      } else {
        await addPropertyInfo(this.proDate)
      }
      this.$emit('renderList')
      this.$message({
        message: this.proDate.id ? '更改成功' : '添加成功',
        type: 'success'
      })
      this.clonse()
    },
    // 关闭弹窗
    clonse() {
      this.$emit('update:showDialog', false)
      this.proDate = {
        title: '',
        desc: '',
        image: '',
        agent: '',
        content: '',
        address: '',
        price: '',
        photos: [],
        tags: [],
        category: ''
      }
      this.$refs.form.resetFields()
    },
    // 标签
    handleClose(tag) {
      this.proDate.tags.splice(this.proDate.tags.indexOf(tag), 1)
    },
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    },
    handleInputConfirm() {
      const inputValue = this.inputValue
      if (inputValue) {
        this.proDate.tags.push(inputValue)
      }
      this.inputVisible = false
      this.inputValue = ''
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
